package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;


    @PostMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithSetmealDish(setmealDto);
        return R.success("添加成功");
    }
    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构建分页
        Page<Setmeal> pageInfo = new Page<>();
        //构建条件
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //模糊查询
        lqw.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        //排序
        lqw.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,lqw);

        //真正返回的page
        Page<SetmealDto> resultPage = new Page<>();
        //除了records  其他都复制
        BeanUtils.copyProperties(pageInfo,resultPage,"records");
        List<Setmeal> records = pageInfo.getRecords();
        ArrayList<SetmealDto> list = new ArrayList<>();

        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record,setmealDto);
            Category category = categoryService.getById(record.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            list.add(setmealDto);
        }
        resultPage.setRecords(list);
        return R.success(resultPage);
    }
        //删除套餐
    @DeleteMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){

        setmealService.delete(ids);
        return R.success("删除成功");
    }

    //启售与停售
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmeal",allEntries = true)
    public R<String> status(@PathVariable int status,@RequestParam List<Long> ids){
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids);
        List<Setmeal> list = setmealService.list(lqw);
        for (Setmeal setmeal : list) {
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("");
    }
    //跟据id查询 用于修改的页面回显
    @GetMapping("/{id}")
    @Cacheable(value = "setmeal",key = "#id", unless = "#result == null")
    public R<SetmealDto> getById(@PathVariable Long id){
        SetmealDto setmealDto=setmealService.getByIdWith(id);
        return R.success(setmealDto);
    }

    //修改套餐
    @PutMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWith(setmealDto);
        return R.success("修改成功");
    }
        @GetMapping("/list")
        @Cacheable(value = "setmeal",key = "#setmeal.categoryId+'_'+#setmeal.status")
        public R<List<Setmeal>> list(Setmeal setmeal){
            LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
            lqw.eq(Setmeal::getStatus,setmeal.getStatus());
            lqw.orderByDesc(Setmeal::getUpdateTime);
            List<Setmeal> list = setmealService.list(lqw);
            return R.success(list);
    }
    @GetMapping("/dish/{id}")
    public R<List<DishDto>> setmealDish(@PathVariable Long id){
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(lqw);
        ArrayList<DishDto> dishDtoList = new ArrayList<>();
        for (SetmealDish setmealDish : list) {
            DishDto dishDto = new DishDto();
            Dish dish = dishService.getById(setmealDish.getDishId());
            BeanUtils.copyProperties(dish,dishDto);
            dishDto.setCopies(setmealDish.getCopies());
            dishDtoList.add(dishDto);
        }
        return R.success(dishDtoList);
    }

}    